/******************************************************************************
 *
 * Project:		mGameEngine
 * File:		connection.h
 * Author:		karooolek
 * Created on:	2009-03-14
 *
 ******************************************************************************/

#ifndef CONNECTION_H_
#define CONNECTION_H_

#include "connectionstate.h"
#include "types.h"

/**
 * Interface for connections with another machines/processes.
 */
class Connection
{
protected:
	/**
	 * Current state.
	 */
	volatile ConnectionState _state;

public:
	/**
	 * Create connection.
	 */
	Connection();

	/**
	 * Destroy connection.
	 */
	virtual ~Connection();

	/**
	 * Open connection.
	 */
	virtual void open() = 0;

	/**
	 * Close connection.
	 */
	virtual void close() = 0;

	/**
	 * Connect to connection server.
	 */
	virtual void connect() = 0;

	/**
	 * Disconnect from connection server.
	 */
	virtual void disconnect() = 0;

	/**
	 * Get connection state.
	 * @return actual connection state
	 */
	ConnectionState getState() const
	{
		return _state;
	}

	/**
	 * Send data to another machine/precess.
	 * @param data data to be sent.
	 * @param size data size.
	 * @return number of sent bytes or 0 if connection was lost.
	 */
	virtual uint send(const uint8 *data, uint size) = 0;

	/**
	 * Receive data from another machine/precess.
	 * @param buffer buffer to store received data.
	 * @param size buffer size.
	 * @return number of received bytes or 0 if connection was lost.
	 */
	virtual uint receive(uint8 *buffer, uint size) = 0;
};

#endif // CONNECTION_H_
